package com.example.beikeyigou.login_reg;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.beikeyigou.R;
import com.example.beikeyigou.login_reg.LoginFragment;
import com.example.beikeyigou.login_reg.LoginVPAdapter;
import com.example.beikeyigou.login_reg.RegisterFragment;

import java.util.ArrayList;
import java.util.List;

public class LoginRegActivity extends AppCompatActivity {
    TextView loginTv, regTv;
    ViewPager loginVp;
    List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_reg);

        // 接收一个传来的数据，表示跳转的位置    0：登录 / 1：注册
        int flag = getIntent().getIntExtra("flag", 0);
        findView();
        setListener();
        initPaper();        // 声明viewpaper的展示页
        setPaperPos(flag);
        initState();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 沉浸式状态栏
     */
    private void initState() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    // 设置显示位置
    private void setPaperPos(int pos) {
        if (pos == 0) {
            loginTv.setTextColor(this.getResources().getColor(R.color.pink));
            regTv.setTextColor(Color.WHITE);
        } else if (pos == 1) {
            regTv.setTextColor(this.getResources().getColor(R.color.pink));
            loginTv.setTextColor(Color.WHITE);
        }
        loginVp.setCurrentItem(pos);
    }

    // 声明viewpaper的展示页
    private void initPaper() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new RegisterFragment());
        // 将fragment放到viewpaper中，展示在activity上
        loginVp.setAdapter(new LoginVPAdapter(getSupportFragmentManager(), fragmentList));
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.login_tv_log:
                    setPaperPos(0);
                    break;
                case R.id.login_tv_reg:
                    setPaperPos(1);
                    break;
            }
        }
    };

    ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            setPaperPos(position);
        }
    };

    // 设置监听器
    private void setListener() {
        loginTv.setOnClickListener(onClickListener);
        regTv.setOnClickListener(onClickListener);
        loginVp.setOnPageChangeListener(listener);
    }

    // 初始化控件
    private void findView() {
        loginTv = findViewById(R.id.login_tv_log);
        regTv = findViewById(R.id.login_tv_reg);
        loginVp = findViewById(R.id.login_vp);
    }
}